﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using System.Xml;
using System.IO;

namespace YXShop.Web.admin.templates.lable
{
    public partial class label_edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                YXShop.Common.PromptInfo.Popedom("011002002", "对不起，您没有权限进行编辑");
                YXShop.Common.PromptInfo.Popedom("011002004", "对不起，您没有权限进行编辑");
                string LabelName = ChangeHope.WebPage.PageRequest.GetQueryString("name");
                string ModuleId = ChangeHope.WebPage.PageRequest.GetQueryString("moduleid");
                //标签类别
                string LabelType = ChangeHope.WebPage.PageRequest.GetQueryString("tid");
                //标签类型
                string LabelClass = ChangeHope.WebPage.PageRequest.GetQueryString("LT");
                ViewState["IsStoreLabel"] = ModuleId;
                if (LabelName != "")
                {
                    bindInfo(LabelName, LabelClass,0);
                }
                if (ModuleId != "")
                {
                    InsertContent(ModuleId);
                    ViewState["ModuleID"] = ModuleId;
                }
                InitWebControl();

            }
        }

        #region 保存信息
        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbSave_Click(object sender, EventArgs e)
        {
            string templatePath = ChangeHope.WebPage.PageRequest.GetQueryString("FilePath");
            int pCount = templatePath.IndexOf('/', 26);
            if (pCount > 0)
            {
                templatePath = templatePath.Substring(0, pCount);
            }
            string path = Server.MapPath("~//" + templatePath + "/label/label.xml");
            if (!File.Exists(path))
            {
                StringBuilder shtml = new StringBuilder();
                shtml.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
                shtml.Append("<templetelabel>\r\n");
                shtml.Append("</templetelabel>\r\n");
                string fileContent = shtml.ToString();
                ChangeHope.Common.FileHelper fh = new ChangeHope.Common.FileHelper();
                fh.CreateFile(path, fileContent);
            }
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);

            bool isExitLabel = false;
            string labelId = "0";
            string labelName = "";
             XmlNodeList nodeLists = xmlDoc.SelectSingleNode("templetelabel").ChildNodes;//获取label节点的所有子节点
             foreach (XmlNode xn in nodeLists)//遍历所有子节点
             {
                 XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
                 labelName = xe.GetAttribute("Name");
                 if (labelName == this.txtLabelName.Text)
                 {
                     labelId = xe.GetAttribute("Id");
                     
                     isExitLabel = true;
                 }
             }

            if (ViewState["ID"] == null)
            {
                if (isExitLabel)
                {
                    this.ltlMsg.Text = "操作失败，标签名不能重复，请重新输入标签名。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionOk";
                    return;
                }
                XmlNode root = xmlDoc.SelectSingleNode("templetelabel");//查找<labeltypegroup>
                int id = 1;
                if (root.ChildNodes.Count > 0)
                {
                    id = int.Parse(root.LastChild.Attributes.GetNamedItem("Id").Value) + 1;
                }
                XmlElement xe1 = xmlDoc.CreateElement("label");//创建一个<label>节点
                xe1.SetAttribute("Id", id.ToString());//设置该节点Id属性
                xe1.SetAttribute("Name", this.txtLabelName.Text);//设置该节点Name属性
                XmlElement xesub1 = xmlDoc.CreateElement("content");
                xesub1.InnerText = this.txtLabelContent.Text;//设置文本节点
                xe1.AppendChild(xesub1);//添加到<label>节点中
                XmlElement xesub2 = xmlDoc.CreateElement("description");
                xesub2.InnerText = this.txtDescription.Text.Trim();
                xe1.AppendChild(xesub2);
                XmlElement xesub3 = xmlDoc.CreateElement("creattime");
                xesub3.InnerText = System.DateTime.Now.ToString();
                xe1.AppendChild(xesub3);
                XmlElement xesub4 = xmlDoc.CreateElement("labeltype");
                xesub4.InnerText = this.ddlLabelClass.SelectedValue;
                xe1.AppendChild(xesub4);
                XmlElement xesub5 = xmlDoc.CreateElement("moduleid");
                xesub5.InnerText = ViewState["ModuleID"].ToString();
                xe1.AppendChild(xesub5);
                root.AppendChild(xe1);//添加到<label>节点中
            }
            else
            {
                if (isExitLabel)
                {
                    if (ViewState["ID"].ToString() != labelId)
                    {
                        this.ltlMsg.Text = "操作失败，标签名不能重复，请重新输入标签名。";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionOk";
                        return;
                    }
                }
                XmlNodeList nodeList = xmlDoc.SelectSingleNode("templetelabel").ChildNodes;//获取label节点的所有子节点
                foreach (XmlNode xn in nodeList)//遍历所有子节点
                {
                    XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
                    if (xe.GetAttribute("Id") == ViewState["ID"].ToString())
                    {
                        xe.SetAttribute("Name", this.txtLabelName.Text);//修改该属性
                        XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
                        foreach (XmlNode xn1 in nls)//遍历
                        {
                            XmlElement xe2 = (XmlElement)xn1;//转换类型
                            if (xe2.Name == "content")//如果找到
                            {
                                xe2.InnerText = this.txtLabelContent.Text.Trim();//则修改
                                continue;//找到后继续
                            }
                            if (xe2.Name == "description")//如果找到
                            {
                                xe2.InnerText = this.txtDescription.Text.Trim();//则修改
                                continue;//找到后继续
                            }
                            if (xe2.Name == "creattime")//如果找到
                            {
                                xe2.InnerText = System.DateTime.Now.ToString();//则修改
                                continue;//找到后继续
                            }
                            if (xe2.Name == "labeltype")//如果找到
                            {
                                xe2.InnerText = this.ddlLabelClass.SelectedValue;//则修改
                                continue;//找到后继续
                            }
                            if (xe2.Name == "moduleid")//如果找到
                            {
                                xe2.InnerText = ViewState["ModuleID"].ToString(); //则修改
                                continue;//找到后继续
                            }
                        }
                        break;
                    }
                }

            }
            xmlDoc.Save(path);

            if (ViewState["ID"] != null)
            {
                    this.ltlMsg.Text = "操作成功，已保存该信息";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionOk";
                    return;
            }
            else
            {
               
                    this.ltlMsg.Text = "操作成功，已保存该信息";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionOk";
                    return;
            }
        }
        #endregion


        #region 绑定标签信息
        /// <summary>
        /// 绑定标签信息
        /// </summary>
        /// <param name="name"></param>
        protected void bindInfo(string name, string labelclass,int StyleId)
        {
            string templatePath = ChangeHope.WebPage.PageRequest.GetQueryString("FilePath");
            int pCount = templatePath.IndexOf('/',26);
            if (pCount > 0)
            {
                templatePath = templatePath.Substring(0, pCount);
            }
            string path = Server.MapPath("~//" + templatePath + "/label/label.xml");
            bool isLabelName = false;
            if (File.Exists(path))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path);
                XmlNodeList nodeList = xmlDoc.SelectSingleNode("templetelabel").ChildNodes;//获取templetelabel节点的所有子节点
                if (nodeList.Count > 0)
                {
                    bindType(false, StyleId);
                    foreach (XmlNode xn in nodeList)//遍历所有子节点
                    {
                        XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
                        if (xe.GetAttribute("Name") == name)
                        {
                            isLabelName = true;
                            this.txtLabelName.Text = xe.GetAttribute("Name");
                            ViewState["ID"] = xe.GetAttribute("Id");
                            XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
                            foreach (XmlNode xn1 in nls)//遍历
                            {
                                XmlElement xe2 = (XmlElement)xn1;//转换类型
                                if (xe2.Name == "content")//如果找到
                                {
                                    this.txtLabelContent.Text = xe2.InnerText;
                                    continue;//找到后继续
                                }
                                if (xe2.Name == "description")//如果找到
                                {
                                    this.txtDescription.Text = xe2.InnerText;
                                    continue;//找到后继续
                                }

                                if (xe2.Name == "labeltype")//如果找到
                                {
                                    this.ddlLabelClass.SelectedValue = xe2.InnerText;
                                    continue;//找到后继续
                                }
                                this.lbLabelType.Text = "#YXShopDefinTag";
                            }
                            break;
                        }
                    }
                    if (!isLabelName)
                    {
                        this.lbLabelType.Text = "#YXShopDefinTag";
                        this.txtLabelName.Text = name;
                    }
                }
                else
                {
                    bindType(true, StyleId);
                    this.lbLabelType.Text = "#YXShopDefinTag";
                    this.txtLabelName.Text = name;
                }

            }
            else
            {
                bindType(true, StyleId);
                this.lbLabelType.Text = "#YXShopDefinTag";
                this.txtLabelName.Text = name;
            }
        }
        #endregion

        /// <summary>
        /// 绑定类型
        /// </summary>
        private void bindType(bool judge,int StyleId)
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            //第一步先添加表头
            string templatePath = ChangeHope.WebPage.PageRequest.GetQueryString("FilePath");
            int pCount = templatePath.IndexOf('/', 26);
            if (pCount > 0)
            {
                templatePath = templatePath.Substring(0, pCount);
            }
            string path = Server.MapPath("~//" + templatePath + "/label/labeltype.xml");
            if (File.Exists(path))
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path);
                XmlNode xn = xmlDoc.SelectSingleNode("labeltypegroup");
                XmlNodeList xnl = xn.ChildNodes;
                if (xnl.Count > 0)
                {
                    foreach (XmlNode xnf in xnl)
                    {
                        XmlElement xe = (XmlElement)xnf;
                        XmlNodeList xnf1 = xe.ChildNodes;
                        foreach (XmlNode xn2 in xnf1)
                        {
                            if (xn2.Name == "name")
                            {
                                this.ddlLabelClass.Items.Add(new ListItem(xn2.InnerText, xe.GetAttribute("id")));
                            }
                        }
                    }

                }
            }
        }

        /// <summary>
        /// 验证
        /// </summary>
        private void InitWebControl()
        {
            ChangeHope.WebPage.WebControl.Validate(this.txtLabelName, "输入标签的名称", "isnull", "必填", "该项为必填项");
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
            string FileName=ChangeHope.WebPage.PageRequest.GetQueryString("FileName");
            string FilePath=ChangeHope.WebPage.PageRequest.GetQueryString("FilePath");
            string styleId = ChangeHope.WebPage.PageRequest.GetString("StyleId");
            if (!string.IsNullOrEmpty(FileName))
            {
                this.hlBack.NavigateUrl = "../amendlable.aspx?dir=~\\" + FilePath + "&filename=" + FileName + "&styleId=" + styleId + "";
                this.hfBottomBack.NavigateUrl = "../amendlable.aspx?dir=~\\" + FilePath + "&filename=" + FileName + "&styleId=" + styleId + "";
            }
            else
            {
                string labelTypeId = ChangeHope.WebPage.PageRequest.GetQueryString("tid");
                this.hlBack.NavigateUrl = "label_list.aspx?q_id=" + labelTypeId + "&q_templatepath=" + FilePath + "&q_name=";
                this.hfBottomBack.NavigateUrl = "label_list.aspx?q_id=" + labelTypeId + "&q_templatepath=" + FilePath + "&q_name=";
            }
        }

        #region 标签信息
        protected void InsertContent(string Ty)
        {
            string str = string.Empty;
            switch (Ty)
            {
                case "1"://商品分类标签
                    str = "<a href=\"javascript:show('ProductClass',document.getElementById('adress'),'选择标签参数(商品分类)',700,300);\" class=\"list_link\" title=\"商品分类\">商品分类</a>";
                    break;
                case "2"://商品品牌
                    str = "<a href=\"javascript:show('ProductBrand',document.getElementById('adress'),'选择标签参数(品牌)',700,300);\" class=\"list_link\" title=\"品牌列表\">品牌列表</a>";
                    break;
                case "3"://商品列表
                    str = "<a href=\"javascript:show('ProductList',document.getElementById('adress'),'选择标签参数(商品列表)',700,500);\" class=\"list_link\" title=\"商品列表\">商品列表</a>&nbsp;&nbsp;<a href=\"javascript:show('ProductProperty',document.getElementById('adress'),'选择标签参数(商品属性)',700,500);\" class=\"list_link\" title=\"商品属性\">商品属性</a>&nbsp;&nbsp;<a href=\"javascript:show('ProductListMode',document.getElementById('adress'),'选择标签参数(商品列表排序及排版切换)',700,400);\" class=\"list_link\" title=\"商品列表排序及排版切换\">商品列表排序及排版切换</a>&nbsp;&nbsp;<a href=\"javascript:show('ProductGroupBuyInfo',document.getElementById('adress'),'选择标签参数(团购商品活动信息)',700,400);\" class=\"list_link\" title=\"团购商品活动信息\">团购商品活动信息</a>";
                    break;
                case "5":
                    str = "<a href=\"javascript:show('NewsList',document.getElementById('adress'),'选择标签参数(列表类)',700,500);\" class=\"list_link\" title=\"列表类\">列表类</a>&nbsp;&nbsp;<a href=\"javascript:show('NewsContent',document.getElementById('adress'),'选择标签参数(文章内容)',700,400);\" class=\"list_link\" title=\"文章内容\">文章内容</a>";
                    break;
                case "6":
                    str = "<a href=\"javascript:show('CommentInfo',document.getElementById('adress'),'选择标签参数(点评信息)',700,500);\" class=\"list_link\" title=\"点评信息\">点评信息</a>&nbsp;&nbsp;<a href=\"javascript:show('ReplyInfo',document.getElementById('adress'),'选择标签参数(回复信息)',700,500);\" class=\"list_link\" title=\"回复信息\">回复信息</a>&nbsp;&nbsp;<a href=\"javascript:show('CommentForm',document.getElementById('adress'),'选择标签参数(点评表单)',700,500);\" class=\"list_link\" title=\"点评表单\">点评表单</a>";
                    break;
                case "8"://商品内容
                    str = "<a href=\"javascript:show('ProductContent',document.getElementById('adress'),'选择标签参数(商品内容页分类)',700,400);\" class=\"list_link\" title=\"商品内容\">商品内容</a>";
                    break;
                case "9"://友情链接
                    str = "<a href=\"javascript:show('Friendlink',document.getElementById('adress'),'选择标签参数(友情链接)',700,300);\" class=\"list_link\" title=\"友情链接\">友情链接</a>";
                    break;
                case "10"://高级搜索
                    str = "<a href=\"javascript:show('AdvancedSearch',document.getElementById('adress'),'选择标签参数(商品搜索)',700,300);\" class=\"list_link\" title=\"商品搜索\">商品搜索</a>";
                    break;
                case "11"://留言信息
                    str = "<a href=\"javascript:show('LeaveWrodInfo',document.getElementById('adress'),'选择标签参数(留言信息)',700,400);\" class=\"list_link\" title=\"留言信息\">留言信息</a>&nbsp;&nbsp;<a href=\"javascript:show('LeaveWrodForm',document.getElementById('adress'),'选择标签参数(留言表单表单)',700,300);\" class=\"list_link\" title=\"留言表单\">留言表单</a>";
                    break;
                case "12"://购物车信息 
                    str = "<a href=\"javascript:show('shoppingcart',document.getElementById('adress'),'选择标签参数(购物车信息)',700,300);\" class=\"list_link\" title=\"购物车信息\">购物车信息</a>&nbsp;&nbsp;<a href=\"javascript:show('usershopinginfo',document.getElementById('adress'),'选择标签参数(购物提供信息)',700,300);\" class=\"list_link\" title=\"购物提供信息\">购物提供信息</a>";
                    break;
                case "13"://商铺信息
                    StringBuilder shopInfo = new StringBuilder();
                    shopInfo.Append("(<a href=\"javascript:show('StoreType',document.getElementById('adress'),'选择标签参数(店铺类型信息)',700,500);\" class=\"list_link\" title=\"店铺类型\">店铺类型</a>-");
                    shopInfo.Append("<a href=\"javascript:show('StoreProductList',document.getElementById('adress'),'选择标签参数(商品列表)',700,500);\" class=\"list_link\" title=\"店铺商品\">店铺商品</a>-");
                    shopInfo.Append("<a href=\"javascript:show('StoreInfo',document.getElementById('adress'),'选择标签参数(商品列表)',700,500);\" title=\"店铺信息\">店铺信息</a>) ");
                    shopInfo.Append("(<a href=\"javascript:show('websitetop',document.getElementById('adress'),'选择标签参数(头部信息)',700,300);\" class=\"list_link\" title=\"头部信息\">头部信息</a>-");
                    shopInfo.Append("<a href=\"javascript:show('websitebottom',document.getElementById('adress'),'选择标签参数(底部信息)',700,300);\" class=\"list_link\" title=\"底部信息\">底部信息</a>) ");
                    shopInfo.Append("(<a href=\"javascript:show('CreateURL',document.getElementById('adress'),'填写参数(URL)',700,300);\" class=\"list_link\" title=\"URL\">生成URL</a>) ");
                    shopInfo.Append("(<a href=\"javascript:show('LeaveWrodInfo',document.getElementById('adress'),'选择标签参数(留言信息)',700,400);\" class=\"list_link\" title=\"留言信息\">留言信息</a>-");
                    shopInfo.Append("<a href=\"javascript:show('LeaveWrodForm',document.getElementById('adress'),'选择标签参数(留言表单表单)',700,300);\" class=\"list_link\" title=\"留言表单\">留言表单</a>) ");
                    shopInfo.Append("(<a href=\"javascript:show('StoreSearch',document.getElementById('adress'),'选择标签参数(店铺搜索表单信息)',700,300);\" class=\"list_link\" title=\"店铺搜索表单\">店铺搜索表单</a>)");
                    shopInfo.Append("<hr style=\"border: 1px dotted #999999;height: 1px;\"/>");
                    shopInfo.Append("(<a href=\"javascript:show('CommentInfo',document.getElementById('adress'),'选择标签参数(点评信息)',700,500);\" class=\"list_link\" title=\"点评信息\">点评信息</a>-<a href=\"javascript:show('ReplyInfo',document.getElementById('adress'),'选择标签参数(回复信息)',700,500);\" class=\"list_link\" title=\"回复信息\">回复信息</a>-<a href=\"javascript:show('CommentForm',document.getElementById('adress'),'选择标签参数(点评表单)',700,500);\" class=\"list_link\" title=\"点评表单\">点评表单</a>)");
                    shopInfo.Append("(<a href=\"javascript:show('Friendlink',document.getElementById('adress'),'选择标签参数(店铺友情链接)',700,300);\" class=\"list_link\" title=\"友情链接\">友情链接</a>) ");
                    str = shopInfo.ToString();
                    break;
                case "98"://网站头部
                    str = "<a href=\"javascript:show('websitetop',document.getElementById('adress'),'选择标签参数(头部信息)',700,300);\" class=\"list_link\" title=\"头部信息\">头部信息</a>";
                    break;
                case "99"://网站底部
                    str = "<a href=\"javascript:show('websitebottom',document.getElementById('adress'),'选择标签参数(底部信息)',700,300);\" class=\"list_link\" title=\"底部信息\">底部信息</a>";
                    break;
                case "100"://URL
                    str = "<a href=\"javascript:show('CreateURL',document.getElementById('adress'),'填写参数(URL)',700,300);\" class=\"list_link\" title=\"URL\">生成URL</a>";
                    break;
                case "101"://商城广告
                    str = "<a href=\"javascript:show('Advertise',document.getElementById('adress'),'选择广告(广告信息)',700,400);\" class=\"list_link\" title=\"广告信息\">广告信息</a>";
                    break;
                case "102"://用户注册  memberagree_parameter.aspx
                    str = "<a href=\"javascript:show('MemberRegForm',document.getElementById('adress'),'选择标签参数(注册表单信息)',700,400);\" class=\"list_link\" title=\"注册信息\">注册表单</a>&nbsp;&nbsp;<a href=\"javascript:show('MemberAgree',document.getElementById('adress'),'选择标签参数(用户协议信息)',700,400);\" class=\"list_link\" title=\"用户协议\">用户协议</a>";
                    break;
                case "21": //会员登陆
                    str = "<a href=\"javascript:show('Login',document.getElementById('adress'),'选择标签参数(会员登陆表单信息)',700,400);\" class=\"list_link\" title=\"登陆信息\">会员登陆表单</a>";
                    break;
                case "106":
                    str = "<a href=\"javascript:show('History',document.getElementById('adress'),'选择标签参数(最近浏览商品)',700,400);\" class=\"list_link\" title=\"最近浏览商品\">最近浏览商品</a>&nbsp;&nbsp;<a href=\"javascript:show('scanproduct',document.getElementById('adress'),'选择标签参数(浏览与购买商品记录)',700,400);\" class=\"list_link\" title=\"浏览与购买商品记录\">浏览与购买商品记录</a>";
                    break;
            }
            this.lbInsertContent.Text = str;

        }
        #endregion

    }
}
